/*
 * Copyright (c) 2009, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "contiki.h"
#include "looci.h"
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include "raven-msg.h"
#endif


#ifdef LOOCI_BLINK_DEBUG
#include <stdio.h>
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else // other target than avr raven
#define PRINTF printf
#endif // end CONTIKI_TARGET_AVR_RAVEN
#else // LOOCI_BLINK_DEBUG is not defined
#define PRINTF(...)
#endif // end LOOCI_BLINK_DEBUG

// printf alike things are currently broken for loadable components on AVR Raven
// Your mileage may vary, so you can try to uncomment this, but don't
// rely on it working.
#ifdef BUILD_COMPONENT
#ifdef CONTIKI_TARGET_AVR_RAVEN
#undef PRINTF
#define PRINTF(...)
#endif // CONTIKI_TARGET_AVR_RAVEN
#endif // BUILD_COMPONENT

// printf for minimal net is not working in loadable components, but log_message can be used
#ifdef BUILD_COMPONENT
#ifdef CONTIKI_TARGET_MINIMAL_NET
#include "log.h"
#undef PRINTF
#define PRINTF(a) log_message("",a)
#endif // end CONTIKI_TARGET_MINIMAL_NET
#endif // end BUILD_COMPONENT

COMPONENT_NO_INTERFACES(blink);
COMPONENT_NO_RECEPTACLES(blink);
COMPONENT(blink, "LED Blink");

LOOCI_COMPONENTS(&blink);

COMPONENT_THREAD(blink, ev, data)
{
  COMPONENT_BEGIN();

  // NOTE
  // printf might not work in a loadable component
  // this depends on whether printf is used in the core or not ...
  PRINTF("Blink starting\r\n");

  static struct etimer et;

  while(1) {
    etimer_set(&et, CLOCK_SECOND * 2);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

#ifdef CONTIKI_TARGET_AVR_RAVEN
    ravenlcd_show_msg(6, "BLINK");
#endif
    // NOTE
    // printf might not work in a loadable component
    // this depends on whether printf is used in the core or not ...
    PRINTF("Blink\r\n"); 
  } 



  COMPONENT_END();
}
